using System.Collections;
using UnityEngine;

public class Humanoid : MonoBehaviour
{
	private int[,] characterTypeList = new int[10, 5]
	{
		{ 0, 0, 0, 0, 0 },
		{ 0, 1, 0, 0, 0 },
		{ 1, 1, 1, 1, 0 },
		{ 2, 1, 2, 2, 0 },
		{ 3, 1, 3, 3, 0 },
		{ 3, 1, 4, 4, 1 },
		{ 5, 1, 5, 5, 1 },
		{ 1, 1, 0, 0, 0 },
		{ 0, 1, 0, 0, 0 },
		{ 1, 1, 0, 0, 0 }
	};

	[HideInInspector]
	public Transform tf;

	[HideInInspector]
	public Rigidbody2D rb;

	[HideInInspector]
	public Animator at;

	[HideInInspector]
	public BoxCollider2D col;

	[HideInInspector]
	public AnimatorOverrideController atoc;

	[HideInInspector]
	public AnimationClipOverrides animClipOverrides;

	[HideInInspector]
	public Transform TF_CenterBone;

	[HideInInspector]
	public Transform TF_Shadow;

	[HideInInspector]
	public Transform TF_IMG_Shadow;

	[HideInInspector]
	public GameObject GO_Blood;

	[HideInInspector]
	public ParticleSystem PS_Blood;

	public Color characterColor;

	public float characterGravity;

	public int runningSpeed;

	public float characterScale;

	public float maxHP;

	public float hitSize;

	public int basicAttackRangeWidth;

	public int basicAttackRangeHeight;

	public int basicAtkPower;

	public int basicDefPower;

	public float basicCriPer;

	public float basicCriRate;

	public int kindOfMotion;

	public bool isVisibleOnScreen;

	[HideInInspector]
	public int grabingWeaponID;

	[HideInInspector]
	public Vector2 attackRange;

	[HideInInspector]
	public Vector2 attackVelocity;

	//[HideInInspector]
	public float hp;

	[HideInInspector]
	public OnTheFloorSensor SCRT_OnTheFloorSensor;

	[HideInInspector]
	public bool currentOnTheFloorFlag;

	[HideInInspector]
	public BoxCollider2D COL_HitBox;

	[HideInInspector]
	public Animator AT_AttackZone;

	[HideInInspector]
	public Transform TF_AttackZone;

	[HideInInspector]
	public BoxCollider2D COL_AttackZone;

	[HideInInspector]
	public CircleCollider2D COL_AttackZoneCircle;

	[HideInInspector]
	public SpriteRenderer SR_AttackZone;

	[HideInInspector]
	public AttackInfo SCRT_AttackInfo;

	[HideInInspector]
	public Rigidbody2D[] RB_IKList;

	[HideInInspector]
	public SpriteRenderer[] SR_BodyParts;

	[HideInInspector]
	public Transform[] TF_InHands;

	[HideInInspector]
	public SpriteRenderer[] SR_InHands;

	[HideInInspector]
	public WeaponAbility SCRT_WeaponAbility;

	[HideInInspector]
	public TrailRenderer Trail_Body;

	[HideInInspector]
	public SpriteRenderer SR_Helm;

	[HideInInspector]
	public Cloak SCRT_Cloak;

	[HideInInspector]
	public Transform TF_Head;

	[HideInInspector]
	public int currentAttackNumber;

	[HideInInspector]
	private Coroutine CO_SlowMotionControl;

	[HideInInspector]
	public bool attackingNow;

	[HideInInspector]
	public float freezingTime;

	[HideInInspector]
	public int temp;

	[HideInInspector]
	public string[] running_NormalList = new string[1] { "StickMan_Run" };

	[HideInInspector]
	public string[] dash_NormalList = new string[1] { "StickMan_Dash" };

	[HideInInspector]
	public string[] attack_NormalList = new string[4] { "StickMan_FrontPunchR", "StickMan_FrontPunchL", "StickMan_FrontKickR", "StickMan_FrontKickL" };

	[HideInInspector]
	public string[] weaponAttack_NormalList = new string[2] { "StickMan_WeaponAttackFirst", "StickMan_WeaponAttackSecond" };

	[HideInInspector]
	public string[] swipeAttack_NormalList = new string[1] { "StickMan_SwipeAttack" };

	[HideInInspector]
	public string[] damaged_NormalList = new string[1] { "StickMan_Damaged" };

	[HideInInspector]
	public string[] landing_NormalList = new string[1] { "StickMan_Landing" };

	[HideInInspector]
	public string[] jumping_NormalList = new string[1] { "StickMan_Jumping" };

	[HideInInspector]
	public string[] throwing_NormalList = new string[1] { "StickMan_Throwing" };

	[HideInInspector]
	public string[] guard_NormalList = new string[1] { "StickMan_Guard" };

	[HideInInspector]
	public string[] motionOfEnemyList = new string[1] { "StickMan_MotionOfEnemy" };

	private void Awake()
	{
		tf = base.transform;
		col = GetComponent<BoxCollider2D>();
		at = tf.GetChild(0).GetComponent<Animator>();
		atoc = new AnimatorOverrideController(at.runtimeAnimatorController);
		at.runtimeAnimatorController = atoc;
		animClipOverrides = new AnimationClipOverrides(atoc.overridesCount);
		atoc.GetOverrides(animClipOverrides);
		SCRT_OnTheFloorSensor = tf.GetComponentInChildren<OnTheFloorSensor>();
		Transform[] array = new Transform[5];
		array[0] = at.transform.GetChild(0);
		array[1] = array[0].GetChild(0).GetChild(1);
		array[2] = array[0].GetChild(0).GetChild(2);
		array[3] = array[0].GetChild(0).GetChild(3);
		array[4] = array[0].GetChild(0).GetChild(4);
		rb = GetComponent<Rigidbody2D>();
		rb.gravityScale = characterGravity;
		TF_CenterBone = array[0];
		TF_Shadow = tf.Find("Shadow");
		TF_IMG_Shadow = TF_Shadow.GetChild(0);
		GO_Blood = array[0].Find("Blood").gameObject;
		PS_Blood = GO_Blood.GetComponent<ParticleSystem>();
		COL_HitBox = array[0].GetChild(0).Find("HitBox").GetComponent<BoxCollider2D>();
		TF_AttackZone = at.transform.Find("AttackZone");
		AT_AttackZone = TF_AttackZone.GetChild(0).GetComponent<Animator>();
		COL_AttackZone = TF_AttackZone.GetChild(0).GetComponent<BoxCollider2D>();
		COL_AttackZoneCircle = TF_AttackZone.GetChild(0).GetComponent<CircleCollider2D>();
		SR_AttackZone = TF_AttackZone.GetChild(0).GetComponent<SpriteRenderer>();
		SCRT_AttackInfo = COL_AttackZone.GetComponent<AttackInfo>();
		SR_BodyParts = new SpriteRenderer[12];
		SR_BodyParts[0] = array[0].GetChild(0).GetChild(0).GetChild(0)
			.GetComponent<SpriteRenderer>();
		SR_BodyParts[1] = array[0].GetChild(0).GetChild(5).GetComponent<SpriteRenderer>();
		SR_BodyParts[2] = array[1].GetChild(0).GetChild(0).GetChild(0)
			.GetComponent<SpriteRenderer>();
		SR_BodyParts[3] = array[1].GetChild(0).GetChild(1).GetComponent<SpriteRenderer>();
		SR_BodyParts[4] = array[1].GetChild(1).GetComponent<SpriteRenderer>();
		SR_BodyParts[5] = array[2].GetChild(0).GetChild(0).GetChild(0)
			.GetComponent<SpriteRenderer>();
		SR_BodyParts[6] = array[2].GetChild(0).GetChild(1).GetComponent<SpriteRenderer>();
		SR_BodyParts[7] = array[2].GetChild(1).GetComponent<SpriteRenderer>();
		SR_BodyParts[8] = array[3].GetChild(1).GetComponent<SpriteRenderer>();
		SR_BodyParts[9] = array[3].GetChild(0).GetChild(1).GetComponent<SpriteRenderer>();
		SR_BodyParts[10] = array[4].GetChild(1).GetComponent<SpriteRenderer>();
		SR_BodyParts[11] = array[4].GetChild(0).GetChild(1).GetComponent<SpriteRenderer>();
		RB_IKList = new Rigidbody2D[6];
		RB_IKList[0] = array[0].GetComponent<Rigidbody2D>();
		RB_IKList[1] = at.transform.GetChild(1).GetComponent<Rigidbody2D>();
		RB_IKList[2] = at.transform.GetChild(2).GetComponent<Rigidbody2D>();
		RB_IKList[3] = at.transform.GetChild(3).GetComponent<Rigidbody2D>();
		RB_IKList[4] = at.transform.GetChild(4).GetComponent<Rigidbody2D>();
		RB_IKList[5] = at.transform.GetChild(5).GetComponent<Rigidbody2D>();
		TF_InHands = new Transform[2];
		TF_InHands[0] = array[1].GetChild(0).GetChild(0).GetChild(0)
			.GetChild(0);
		TF_InHands[1] = array[2].GetChild(0).GetChild(0).GetChild(0)
			.GetChild(0);
		SR_InHands = new SpriteRenderer[2];
		SR_InHands[0] = TF_InHands[0].GetComponent<SpriteRenderer>();
		SR_InHands[1] = TF_InHands[1].GetComponent<SpriteRenderer>();
		Trail_Body = array[0].GetChild(1).GetComponent<TrailRenderer>();
		TF_Head = SR_BodyParts[0].transform;
		if (SR_BodyParts[0].transform.childCount > 0) SR_Helm = SR_BodyParts[0].transform.GetChild(0).GetComponent<SpriteRenderer>();
		if ((bool)array[0].GetChild(0).GetChild(0).Find("Cloak"))
		{
			SCRT_Cloak = array[0].GetChild(0).GetChild(0).GetChild(1)
				.GetComponent<Cloak>();
		}
	}

	protected void DestroyInHand()
	{
		for (int i = 0; i < TF_InHands[0].childCount; i++)
		{
			Object.Destroy(TF_InHands[0].GetChild(i).gameObject);
		}
	}

	public void AttackZoneColOn(bool swt, string kindOfCOL = "BoxCol")
	{
		if (swt)
		{
			if (kindOfCOL == "BoxCol")
			{
				COL_AttackZone.enabled = true;
			}
			else
			{
				COL_AttackZoneCircle.enabled = true;
			}
		}
		else
		{
			COL_AttackZone.enabled = false;
			COL_AttackZoneCircle.enabled = false;
		}
	}

	public void SetFreezingTIme(float time)
	{
		if (!(freezingTime > 0f))
		{
			freezingTime = time;
			at.Play("StickMan_Idle", -1, 1f);
		}
	}

	public void HideAttackZone()
	{
		AT_AttackZone.Play("AttackZoneShow", -1, 1f);
	}

	public void ShowAttackRange(string kind = "boxCol", float addRangeX = 0f, float addRangeY = 0f)
	{
		if (kind == "boxCol")
		{
			COL_AttackZone.size = new Vector2(attackRange.x + 1f + addRangeX, attackRange.y + addRangeY);
			SR_AttackZone.sprite = Sprites.instance.etcSpriteList[2];
			SR_AttackZone.size = COL_AttackZone.size;
			TF_AttackZone.localPosition = new Vector2((0f - COL_AttackZone.size.x) * 0.5f, 2f);
		}
		else
		{
			COL_AttackZoneCircle.radius = attackRange.x * 0.5f;
			SR_AttackZone.sprite = Sprites.instance.etcSpriteList[3];
			SR_AttackZone.size = new Vector2(COL_AttackZoneCircle.radius * 2f + 2f, COL_AttackZoneCircle.radius * 2f + 2f);
			TF_AttackZone.localPosition = new Vector2(0f, 2f);
		}
		AT_AttackZone.speed = 1f - GM.instance.enemySpeedTimeByLevel;
		AT_AttackZone.Play("AttackZoneShow", -1, 0f);
	}

	public Vector2 AttackRange()
	{
		return (!(SCRT_WeaponAbility != null)) ? new Vector2(basicAttackRangeWidth, basicAttackRangeHeight) : new Vector2(SCRT_WeaponAbility.attackRangeWidth, SCRT_WeaponAbility.attackRangeHeight);
	}

	public int AttackPower()
	{
		return (int)((float)((!(SCRT_WeaponAbility == null)) ? SCRT_WeaponAbility.attackPower : basicAtkPower) * Random.Range(1f, 1.5f));
	}

	public bool PlayerInTheEnemyAttackZone(int arrayNumForAllObjectsInfo)
	{
		if (Player.instance.tf.position.x - Player.instance.hitSize < AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].pos.x + attackRange.x && Player.instance.tf.position.x + Player.instance.hitSize > AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].pos.x - attackRange.x && Player.instance.tf.position.y - Player.instance.hitSize - 1f < AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].pos.y + attackRange.y && Player.instance.tf.position.y + Player.instance.hitSize + 1f > AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].pos.y - attackRange.y)
		{
			return true;
		}
		return false;
	}

	public int GetKindOfWeaponNumber()
	{
		if (SCRT_WeaponAbility != null)
		{
			return SCRT_WeaponAbility.kindOfWeapon;
		}
		return 0;
	}

	public void SetAttackInfo(int kindOfWeapon, int power, Vector2 force)
	{
		SCRT_AttackInfo.kindOfWeapon = kindOfWeapon;
		SCRT_AttackInfo.power = power;
		SCRT_AttackInfo.force = force;
	}

	public AttackInfo GetAttackInfo()
	{
		return SCRT_AttackInfo;
	}

	public void IKDynamicOn(bool dynamicSwt, Vector2 forceVel)
	{
		forceVel *= 2f;
		for (int i = 0; i < RB_IKList.Length; i++)
		{
			if (dynamicSwt)
			{
				RB_IKList[i].gravityScale = VariousInfo.ikGravityScale[i];
				if (i == 0)
				{
					RB_IKList[i].velocity = forceVel;
				}
				else
				{
					RB_IKList[i].velocity = new Vector2(Random.Range(forceVel.x, forceVel.x + 5f), Random.Range(forceVel.y - 2f, forceVel.y + 3f));
				}
			}
			else
			{
				RB_IKList[i].gravityScale = 0f;
				RB_IKList[i].velocity = Vector2.zero;
			}
		}
		rb.velocity = forceVel;
	}

	public void EquipGetOrDrop(string who, bool get, int kindOfEquip = 0, int selectedEquipID = 0)
	{
		if (get)
		{
			if (kindOfEquip > 0)
			{
				SCRT_WeaponAbility = ((kindOfEquip != 1) ? Object.Instantiate(Generator.instance.pool.ArmorPrefabList[selectedEquipID]).GetComponent<WeaponAbility>() : Object.Instantiate(Generator.instance.pool.WeaponPrefabList[selectedEquipID]).GetComponent<WeaponAbility>());
				SCRT_WeaponAbility.Get(who, TF_InHands[0], selectedEquipID);
			}
		}
		else if (SCRT_WeaponAbility != null)
		{
			SCRT_WeaponAbility.Drop();
			SCRT_WeaponAbility = null;
			grabingWeaponID = 0;
		}
		rb.gravityScale = ((kindOfEquip != 1 || Calc.KindOfWeapon(selectedEquipID) != 6) ? characterGravity : 0f);
		attackRange = AttackRange();
	}

	public void BodyTrailSwtich(float width)
	{
		Trail_Body.widthMultiplier = width;
	}

	public void ChangeWeaponTrailShowingTime(float time)
	{
		if (SCRT_WeaponAbility != null)
		{
			SCRT_WeaponAbility.ChangeWeaponTrailShowingTime(time);
		}
	}

	public void ChangeAnimatorSpeed(float speed)
	{
		at.speed = speed;
	}

	public void SlowMotionControl(float duration)
	{
		if (CO_SlowMotionControl != null)
		{
			StopCoroutine(CO_SlowMotionControl);
		}
		if (duration == 0f)
		{
			at.speed = 1f;
		}
		else
		{
			CO_SlowMotionControl = StartCoroutine(IE_SlowMotionControl(duration));
		}
	}

	private IEnumerator IE_SlowMotionControl(float duration)
	{
		at.speed = 0.1f;
		duration -= GM.instance.enemySpeedTimeByLevel;
		if (duration < 0f)
		{
			duration = 0f;
		}
		yield return new WaitForSeconds(duration);
		at.speed = 1f;
	}

	public bool CheckAction()
	{
		if (at.GetCurrentAnimatorStateInfo(0).IsName("StickMan_Idle") || at.GetCurrentAnimatorStateInfo(0).IsName("StickMan_Run") || at.GetCurrentAnimatorStateInfo(0).IsName("StickMan_Jumping") || at.GetCurrentAnimatorStateInfo(0).IsName("StickMan_JumpingLoop"))
		{
			return false;
		}
		return true;
	}

	public void ChangeCharacterAnimType(int type)
	{
		animClipOverrides["StickMan_Idle0"] = AnimClips.instance.idle[characterTypeList[type, 0]];
		animClipOverrides["StickMan_Run0"] = AnimClips.instance.run[characterTypeList[type, 1]];
		animClipOverrides["StickMan_WeaponAttackFirst0"] = AnimClips.instance.weaponAttackFirst[characterTypeList[type, 2]];
		animClipOverrides["StickMan_WeaponAttackSecond0"] = AnimClips.instance.weaponAttackSecond[characterTypeList[type, 3]];
		animClipOverrides["StickMan_Throwing0"] = AnimClips.instance.throwing[characterTypeList[type, 4]];
		atoc.ApplyOverrides(animClipOverrides);
	}

	public void SetColor()
	{
		for (int i = 0; i < SR_BodyParts.Length; i++)
		{
			SR_BodyParts[i].color = characterColor;
		}
		ParticleSystem.MainModule main = PS_Blood.main;
		if (PlayerPrefs.GetInt("forAdult") == 0)
		{
			main.startColor = new ParticleSystem.MinMaxGradient(new Color(1f, 0f, 1f), new Color(0.3f, 0f, 0.3f));
		}
		else
		{
			main.startColor = new ParticleSystem.MinMaxGradient(new Color(1f, 0f, 0f), new Color(0.3f, 0f, 0f));
		}
	}
}
